//
//  ReplacePhoneCodeViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/4/12.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "ReplacePhoneCodeViewController.h"
#import "GTVerifyCodeView.h"
@interface ReplacePhoneCodeViewController (){
    dispatch_source_t _timer;
}
@property (nonatomic, weak) GTVerifyCodeView *codeView;
@property (nonatomic,strong) UIButton *getCodeBtn;
@end

@implementation ReplacePhoneCodeViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"更换手机号";
    [self setUpUI];
    [self clickMethod:self.getCodeBtn];
}

- (void)setUpUI
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight + 1);
    scrollView.backgroundColor = WB_RGBColor(255, 255, 255);
    [self.view addSubview:scrollView];
    
    //提示
    UILabel *titleLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, kScreenWidth, 20)];
    titleLb.text = @"我们已发送验证码到您的手机：";
    titleLb.textAlignment = NSTextAlignmentCenter;
    titleLb.font = [UIFont systemFontOfSize:15];
    titleLb.textColor = WB_HEX_COLOR(0x555555);
    [scrollView addSubview:titleLb];
    
    //号码
    UILabel *phoneLb = [[UILabel alloc] initWithFrame:CGRectMake(0, titleLb.mm_bottom + 20, kScreenWidth, 40)];
    phoneLb.text = self.phone;
    phoneLb.textAlignment = NSTextAlignmentCenter;
    phoneLb.font =  [UIFont systemFontOfSize:22];
    phoneLb.textColor = WB_HEX_COLOR(0x3b8bfe);
    [scrollView addSubview:phoneLb];
    
    GTVerifyCodeView *codeView = [[GTVerifyCodeView alloc] initWithFrame:CGRectMake(0, phoneLb.mm_bottom + 50, kScreenWidth, 50) onFinishedEnterCode:^(NSString *code) {
        NSLog(@"%@",code);
        //        if (arc4random()%2) {
//        [self reset];
        //        }
        [self replacePhone:code];
        
    }];
    [scrollView addSubview:codeView];
    _codeView = codeView;
    
    //获取验证码按钮
    UIButton *getCodeBtn = [UIButton ba_buttonWithFrame:CGRectMake(50, 0, 200, 21) title:@"获取验证码" titleColor:WB_RGBColor(181, 181, 181) titleFont:[UIFont systemFontOfSize:12]];
    getCodeBtn.center = CGPointMake(kScreenWidth / 2, codeView.mm_bottom + 40);
    _getCodeBtn = getCodeBtn;
    [getCodeBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:getCodeBtn]; 

}

- (void)clickMethod:(UIButton *)sender
{

    [XYHttpRequest dataRequestGetMessageWithPhone:self.phone codeLength:4 success:^(id responseObj) {
        
        if (Success) {
            WB_DISPATCH_MAIN_THREAD(^{
                _timer = [Factory startTimeWithBtn:_getCodeBtn];
            });
        }
    } failure:^(NSError *error) {
        
    }];
    
}

//重置验证码
- (void)reset {
    [_codeView resetDefaultStatus];
}

- (void)replacePhone:(NSString*)code
{
    
    [XYHttpRequest dataRequestWithModifiedPhoneNumber:self.phone code:code uccess:^(id responseObj) {
        if (Success) {
            WB_DISPATCH_MAIN_THREAD(^{
                [self.navigationController popViewControllerAnimated:YES];
            })
        }else{
            [NSObject wb_showAlertWithTitle:responseObj[@"info"]];
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
